/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package main.java.cn.webrx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: jse2204 - Ex4
 * <p>Powered by webrx On 2022-09-23 09:32:57
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex4 {
    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("[a-zA-Z]{2,}", Pattern.MULTILINE);
        int n = 0;
        List<String> list = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();

        //实现读取文件，并选出所有单词
        try (var is = new FileInputStream("src.md")) {
            String src = new String(is.readAllBytes());
            Matcher matcher = pattern.matcher(src);
            while (matcher.find()) {
                ++n;
                String word = matcher.group();
                list.add(word);

                if (map.containsKey(word)) {
                    map.put(word, map.get(word) + 1);
                } else {
                    map.put(word, 1);
                }

                //System.out.printf("%s ",word);
                if (n % 20 == 0) {
                    //System.out.println();
                }
            }
        } catch (Exception e) {

        }

        //System.out.println(list.size());
        //System.out.println(list);

        //System.out.println(new HashSet(list).size());

        //System.out.println(map);

        //TreeMap<Integer,String> tmap = new TreeMap<>();
        //tmap.put(3,"ccc");
        //tmap.put(3,"Serializable");
        //System.out.println(tmap.size());

        TreeSet<Word> tset = new TreeSet<>((a,b)->b.getNum()-a.getNum());
        int t = 0;
        List<Word> lok = new ArrayList<>();
        for(String key : map.keySet()){
            //System.out.println(t++);

            Word ww = new Word(key,map.get(key));
            lok.add(ww);
            //System.out.println();
            //tset.add(new Word(key,map.get(key)));
            //tset.add(ww);
        }

        System.out.println(lok.size());
        Collections.sort(lok,(a,b)->b.getNum()-a.getNum());
        for(Word w : lok){
            //System.out.println(w);
            try {

                FileOutputStream fos = new FileOutputStream("c:/java01.txt",true);
                if(w.getNum()>=30){
                    fos.write(w.getName().concat(" ").getBytes());
                }

                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //System.out.println(tset.size());
        for(Word w : tset){
            //System.out.println(w);
        }




    }
}

class Word implements Comparable<Word>{
    private String name;
    private int num;
    public Word(String name,int num){
        this.name = name;
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Word word)) return false;
        return getNum() == word.getNum() && Objects.equals(getName(), word.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getNum());
    }

    @Override
    public int compareTo(Word o) {
        return o.num - this.num;
    }

    @Override
    public String toString() {
        return "Word{" +
                "name='" + name + '\'' +
                ", num=" + num +
                '}';
    }
}
